using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Markup;
using Common;
using Common.Enum;
using device;
using SExperiment;

namespace MainView.Windows
{
    public partial class DeviceSelectorWindow : Window, IComponentConnector
    {
        public ObservableCollection<Experiment> ExperimentList { get; set; }

        public List<RS232Port> Ports { get; set; }

        public int SelectedIndex { get; set; }

        public DeviceSelectorWindow()
        {
            InitializeComponent();
        }

        private void Root_Loaded(object sender, RoutedEventArgs e)
        {
            List<List<string>> list = new List<List<string>>();
            foreach (RS232Port port in Ports)
            {
                List<string> list2 = new List<string>();
                list2.Add(port.PortObject.PortName);
                list2.Add(port.DeviceModel);
                list2.Add(port.DeviceNo);
                if (string.IsNullOrEmpty(port.Note))
                {
                    port.Note = ConfigReader.GetInstance().GetDeviceRemark(port.DeviceNo);
                }
                list2.Add(port.Note);
                if (ExperimentList != null)
                {
                    List<Experiment> list3 = ExperimentList.Where((Experiment s) => s.Status != EExperimentStatus.Completed && s.Device.Series == port.DeviceNo).ToList();
                    if (port.DeviceModel == Device.DeviceModel_96P || port.DeviceModel == Device.DeviceModel_40P)
                    {
                        if (list3.Count == 0)
                        {
                            list2.Add(Utility.GetMessage("NotUse"));
                        }
                        else if (list3.Count >= 2)
                        {
                            list2.Add(Utility.GetMessage("Used"));
                        }
                        else if (list3[0].UseModelA && list3[0].UseModelB)
                        {
                            list2.Add(Utility.GetMessage("Used"));
                        }
                        else if (list3[0].UseModelA)
                        {
                            list2.Add(Utility.GetMessage("Used") + Utility.GetMessage("module_A"));
                        }
                        else if (list3[0].UseModelB)
                        {
                            list2.Add(Utility.GetMessage("Used") + Utility.GetMessage("module_B"));
                        }
                    }
                    else if (port.DeviceModel == Device.DeviceModel_96S || port.DeviceModel == Device.DeviceModel_48P || port.DeviceModel == Device.DeviceModel_24P)
                    {
                        if (list3.Count == 0)
                        {
                            list2.Add(Utility.GetMessage("NotUse"));
                        }
                        else
                        {
                            list2.Add(Utility.GetMessage("Used"));
                        }
                    }
                    else
                    {
                        list2.Add(string.Empty);
                    }
                }
                else if (port.DeviceModel == Device.DeviceModel_96P || port.DeviceModel == Device.DeviceModel_40P)
                {
                    if (port.BlockAState == 0 && port.BlockBState == 0)
                    {
                        list2.Add(Utility.GetMessage("NotUse"));
                    }
                    else if (port.BlockAState == 1 && port.BlockBState == 1)
                    {
                        list2.Add(Utility.GetMessage("Used"));
                    }
                    else if (port.BlockAState == 1 && port.BlockBState == 0)
                    {
                        list2.Add(Utility.GetMessage("Used") + Utility.GetMessage("module_A"));
                    }
                    else if (port.BlockAState == 0 && port.BlockBState == 1)
                    {
                        list2.Add(Utility.GetMessage("Used") + Utility.GetMessage("module_B"));
                    }
                }
                else if (port.DeviceModel == Device.DeviceModel_96S || port.DeviceModel == Device.DeviceModel_48P || port.DeviceModel == Device.DeviceModel_24P)
                {
                    if (port.BlockAState == 0)
                    {
                        list2.Add(Utility.GetMessage("NotUse"));
                    }
                    else
                    {
                        list2.Add(Utility.GetMessage("Used"));
                    }
                }
                else
                {
                    list2.Add(string.Empty);
                }
                list.Add(list2);
            }
            dataGrid.ItemsSource = list;
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            SelectedIndex = dataGrid.SelectedIndex;
            List<List<string>> list = dataGrid.ItemsSource as List<List<string>>;
            if (list != null)
            {
                foreach (List<string> item in list)
                {
                    ConfigReader.GetInstance().SetDeviceRemark(item[2], item[3]);
                }
            }
            base.DialogResult = true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
        }
    }
}
